import {getUserList,getCountUser} from '../../../util/axios'
const state = {
  userList:[],
  size:2,
  page:1,
  count:0
}

const getters = {
  vgetUserList(state){
    console.log(state.userList,'aaaaaaaaaa')
    return state.userList
  },
  getCount(state){
    return state.count
  },
  getSize(state){
    return state.size
  }
}

const mutations = {
  REQ_USERLIST(state,payload){
    state.userList = payload
  },
  REQ_COUNTUSER(state,payload){
    state.count = payload
  },
  CHANGE_PAGE(state,payload){
    state.page = payload
  }
}

const actions = {
  getUserListAction(context){
    getUserList({
      size:context.state.size,
      page:context.state.page
    }).then(res=>{
      console.log(res.data.list)
      if(res.data.code==200){
        context.commit('REQ_USERLIST',res.data.list)
        let list = res.data.list? res.data.list : []
        if(context.state.page !=1 && list.length==0 ){
          context.dispatch('changePageAction',context.state.page-1)
      }
      }
     
    })
  },
  getUserCountAction({commit}){
    getCountUser().then(res=>{
      commit('REQ_COUNTUSER',res.data.list[0].total)
    })
  },
  changePageAction(context,payload){
    context.commit('CHANGE_PAGE',payload)
    context.dispatch('getUserListAction')
  }
}

export default {
  state,
  getters,
  mutations,
  actions,
  namespaced:true
}